package com.geek.javase.wcy04.HomeworkList;

/**
 * @author wangchaoyang
 * @desc TODO
 * @date 2025/9/4
 */
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class LoginTest {
    public static void main(String[] args) {
        List<User> userList = new ArrayList<>();
        userList.add(new User("user1", "123456"));
        userList.add(new User("user2", "abc123"));
        userList.add(new User("admin", "admin123"));
        userList.add(new User("test", "test123"));
        userList.add(new User("guest", "guest123"));

        Scanner scanner = new Scanner(System.in);
        int chance = 3; // 3. 三次输入机会
        while (chance > 0) {
            System.out.print("请输入用户名：");
            String inputUsername = scanner.nextLine();
            System.out.print("请输入密码：");
            String inputPassword = scanner.nextLine();

            boolean isLoginSuccess = false; // 标记是否登录成功
            // 遍历集合，与已有用户信息比对
            for (User user : userList) {
                if (user.getUsername().equals(inputUsername)
                        && user.getPassword().equals(inputPassword)) {
                    isLoginSuccess = true;
                    break;
                }
            }
            if (isLoginSuccess) {
                System.out.println("登录成功！");
                return;
            } else {
                chance--;
                System.out.println("用户名或密码错误，还剩 " + chance + " 次机会。");
            }
        }
        System.out.println("三次机会已用完，登录失败！");
    }
}